//
//  StoreViewController.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/14.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "StoreViewController.h"

@interface StoreViewController (){
    
    NSArray* goodsModelArray;
    NSArray* pointGoodsModelArray;
    NSArray* sliderModelArray;
    UICollectionView* collectionView;
    NSMutableArray* data;
   
    UIView * titleView;
    UIImageView *goodsImage;
    UILabel *_goodsNameLabel ;
    
  
    StoreHeaderView *storeHeaderView;
}

@end

@implementation StoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    
    titleView = UIView.new;
    titleView.backgroundColor = MAIN_COLOR;
    [self.view addSubview:titleView];
    
    
    int height = NAVIGATION_BAR_HEIGHT;
     int collectionViewHeight = MAIN_SCREEN_HEIGHT - height;
    int titleLabelOffset = 20;
    if (DEVICE_IS_IPHONEX) {
        height = NAVIGATION_BAR_X_HEIGHT;
        titleLabelOffset = 40;
        collectionViewHeight = MAIN_SCREEN_HEIGHT - height ;
    }
    
    [titleView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.top);
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.height.equalTo(height);
    }];
    
    //积分商城
    UILabel *labelView = UILabel.new;
    labelView.text = @"积分商城";
    labelView.textColor = [UIColor whiteColor];
    labelView.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:labelView];
    labelView.font = [UIFont systemFontOfSize:20.0];
    
    [labelView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->titleView.top).offset(titleLabelOffset);
        make.height.equalTo(44);
        make.width.equalTo(MAIN_SCREEN_WIDTH);
    }];
    
    //实现UICollectionView需要先初始化flowLayout
    UICollectionViewFlowLayout *flowLayout = UICollectionViewFlowLayout.new;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // 创建collectionView
    collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, height, MAIN_SCREEN_WIDTH, collectionViewHeight) collectionViewLayout:flowLayout];
    //代理协议
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.collectionViewLayout = flowLayout;
    collectionView.alwaysBounceVertical = YES;
    /**
     注册item和区头视图、区尾视图，需要在之后的方法中返回并实现
     */
    [collectionView registerClass:[PointMallViewCell class] forCellWithReuseIdentifier:@"PointMallViewCell"];
   [collectionView registerClass:[StoreHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderIdentifier"];


    [self.view addSubview:collectionView];
    
    [self getPointIndex];
    
    
}


-(void)getPointIndex{
    [HttpHelper request:@"point/index" :@"GET" :nil successBlock:^(NSDictionary *responseDic) {
        
    
        self->sliderModelArray = [SliderModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"][@"slider"]];
        
        
        self->pointGoodsModelArray = [PointGoodsModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"][@"list"]];
        
        //self->data =[NSMutableArray arrayWithArray:self->pointGoodsModelArray];
        
        
        NSMutableArray * sliderImgUrl = NSMutableArray.new;
        for (SliderModel* slider in self->sliderModelArray) {
            [sliderImgUrl addObject:slider.img_url];
        }
        

        [self->storeHeaderView setSlider: self->sliderModelArray];
        [self->storeHeaderView setSliderImgUrl:sliderImgUrl];
       //在请求数据和解析数据后一定要让collectionView重新刷新
        [self->collectionView reloadData];
      
    } failureBlock:^(NSError *error) {
        NSLog(@"Failure!!");
    }];
    
}




- ( UICollectionViewCell *)collectionView:( UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath {
    
   // static NSString * CellIdentifier = @"PointMallViewCell";
   
    PointMallViewCell* cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"PointMallViewCell" forIndexPath: indexPath];
  
    //int  index = indexPath.row;
    
    //将pointGoodsModelArray按indexpath.row存入pointGoodsModel
    //这样才能按item的顺序填入数据
    PointGoodsModel *pointGoodsModel = [pointGoodsModelArray objectAtIndex:indexPath.row];
    
    //set方法传值给cell
   [cell setModel:pointGoodsModel];
    
        return cell;
    
}


//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [pointGoodsModelArray count];
}


//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(MAIN_SCREEN_WIDTH/2, MAIN_SCREEN_WIDTH/2 + 80);//每个cell的大小
}


//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark ---------------------------------CollectionView 每个section中不同的行之间的行间距,如果不实现这两个方法，collectionView的item之间会有默认间距，导致其一格只能放一行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark ---------------------------CollectionView  每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//返回collection的头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
  //  UICollectionReusableView *reusableView = nil;
//
//    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
//
//        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
//                                                          withReuseIdentifier:@"CollectionFooterIdentifier"
//                                                                 forIndexPath:indexPath];
//    } else if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        self->storeHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                          withReuseIdentifier:@"CollectionHeaderIdentifier"
                                                                 forIndexPath:indexPath];
        
//    }
    
    return self->storeHeaderView;
}


// 设置UICollectionView 头视图的宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size={MAIN_SCREEN_WIDTH, 180 };
    return size;
}


//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PointGoodsModel *pointGoodsModel = [self->pointGoodsModelArray objectAtIndex:indexPath.row];
    PointGoodsViewController *pointsGoodsView = [[PointGoodsViewController alloc]init];
    
    //根据id的不同使点击入不同item时所显示的内容不同，首先取出pointsGoodsModel中的
    //id数据，将其复制给PointGoodsViewController的id属性，在PointGoodsViewController请求数据时参数带上id的值即可获得所在页面的数据
    
    pointsGoodsView.id = pointGoodsModel.id;
    //pointsGoodsView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pointsGoodsView animated:YES];
    
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
